<?php

class Roster_Model extends CI_Model {

	private $USER_TABLE = 'users';
	
	private $table;

	private function get_user_type($type) {
		$select_users = array(0 => 'account', 1 => 'order', 2 => 'description');
		$this->db->select($select_users);
		$this->db->where('order', $type);

		$query = $this->db->get($this->USER_TABLE);
		$rows = $query->result_array();

		return $rows;
	}
	
	// Get all users with leader powers from the table
	function get_leaders() {
		return $this->get_user_type('leader');
	}

	// Get all users with admin powers from the table
	function get_admins() {
		return $this->get_user_type('admin');
	}
	
	//get normal users
	function get_users() {
		return $this->get_user_type('user');
	}
	
	function get_users_table() {
		return $this->table;
	}
	
	function refresh_users_table() {
		$select_array = array(0 => 'account', 1 => 'order'); 
		
		//Fetch columns indicated in select_array
		$this->db->select($select_array);
		$this->db->order_by('account', 'desc');
		
		//get results from db
		$query = $this->db->get($this->USER_TABLE);
		
		//Build multidimensional associative array
		$this->table = array(); //Initialize table as empty array
		foreach($query->result_array() as $row){
			$name = $row['account']; //This is going to be the row index
			
			$javascript = 'setOrder(\'' . $name . '\', \'' . $row['order'] . '\'); new Boxy(\'#hidden_form\', {modal : true, draggable : false, closeable : false, title : \'Modify Access Level - '.$name.'\'});';
			$anchor = '<a style="cursor:pointer" onclick="'.$javascript.'">EDIT</a>';
			
			if($row['order'] == 'leader') $anchor = '--'; //Leader cannot edit his own access level
			$this->table[$name] = array_merge($row, array('edit' => $anchor)); //Initialize the row with key username
			
			//Set custom columns
			$this->table[$name]['account'] = anchor('profile/show/'.$name,$name);	
		}
	}

}

?>
